#!/usr/bin/make -f
# Copyright 1994,1995 by Ian Jackson.
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)

# Copyright 2007 by Jurjen Stellingwerff <jurjen@stwerff.xs4all.nl>

# This file is written so you only need to change the package variable to
# the game you are writing on the basis of the moros architecture

package = moros
package2 = $(package)-central
dir1 = debian/$(package)
dir2 = debian/$(package2)
docdir = $(dir1)/usr/share/doc/$(package)
docdir2 = $(dir2)/usr/share/doc/$(package2)

CC = gcc
CFLAGS = -g -Wall
INSTALL_PROGRAM = install

ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
  INSTALL_PROGRAM += -s
endif

build:
	$(checkdir)
	./configure --prefix=/usr
	$(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)"
	touch build

clean:
	$(checkdir)
	rm -f build
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf *~ debian/tmp debian/*~ debian/files* debian/substvars

binary-indep:   checkroot build
	$(checkdir)

binary-arch:    checkroot build
	$(checkdir)
	rm -rf $(dir1) $(dir2)
	install -d $(dir1)/DEBIAN $(dir2)/DEBIAN \
		$(docdir) $(docdir2) \
		$(dir1)/usr/lib/$(package) \
		$(dir1)/usr/share \
		$(dir2)/usr/bin \
		$(dir2)/usr/share/man/man1 \
		$(dir2)/var/lib/$(package) \
		$(dir2)/etc/init.d \
		$(dir2)/etc/cron.daily \
		$(dir2)/etc/logrotate.d

	install -m 755 debian/postinst debian/prerm $(dir1)/DEBIAN
	install -m 755 debian/postinst.$(package2) $(dir2)/DEBIAN/postinst
	install -m 755 debian/prerm.$(package2) $(dir2)/DEBIAN/prerm

	$(MAKE) INSTALL_PROGRAM="$(INSTALL_PROGRAM)" \
	        prefix=$$(pwd)/$(dir1)/usr install

	rm -r $(dir1)/usr/doc

	mv $(dir1)/usr/share/$(package).glade $(dir1)/usr/lib/$(package)
	mv $(dir1)/usr/share/*.jpg $(dir1)/usr/lib/$(package)
	cp $(dir1)/usr/share/ca.pem $(dir1)/usr/lib/$(package)

	mv $(dir1)/usr/share/man/man1/$(package2)* $(dir2)/usr/share/man/man1
	mv $(dir1)/usr/bin/$(package2) $(dir2)/usr/bin
	mv $(dir1)/usr/share/*.pem $(dir2)/var/lib/$(package)
	mv $(dir1)/usr/bin/moros_central.init $(dir2)/etc/init.d/moros_central
	mv $(dir1)/usr/bin/moros_central.cron $(dir2)/etc/cron.daily/moros_central
	mv $(dir1)/usr/share/moros_central.logrot $(dir2)/etc/logrotate.d/moros_central

	cp -a NEWS README debian/copyright $(docdir)
	cp -a NEWS README debian/copyright $(docdir2)
	cp -a debian/changelog $(docdir)/changelog.Debian
	cp -a debian/changelog $(docdir2)/changelog.Debian
	cp -a ChangeLog $(docdir)/changelog
	cp -a ChangeLog $(docdir2)/changelog
	cd $(docdir) && gzip -9 changelog changelog.Debian
	cd $(docdir2) && gzip -9 changelog changelog.Debian
	gzip -r9 $(dir1)/usr/share/man
	gzip -r9 $(dir2)/usr/share/man

	dpkg-shlibdeps $(dir1)/usr/bin/$(package) $(dir1)/usr/bin/$(package)-chatserver $(dir1)/usr/bin/$(package)-server
	dpkg-shlibdeps -pcentral $(dir2)/usr/bin/$(package2)
	dpkg-gencontrol -p$(package) -P$(dir1)
	dpkg-gencontrol -p$(package2) -P$(dir2)
	chown -R root:root $(dir1) $(dir2)
	chmod -R u+w,go=rX $(dir1) $(dir2)
	dpkg --build $(dir1) ..
	dpkg --build $(dir2) ..

define checkdir
	test -f src/client/$(package).cc -a -f debian/rules
endef

binary: binary-indep binary-arch

checkroot:
	$(checkdir)
	test $$(id -u) = 0

.PHONY: binary binary-arch binary-indep clean checkroot
